package com.ruoyi.system.service.impl;

import java.util.*;

import com.ruoyi.system.domain.SalonOrder;
import com.ruoyi.system.domain.SalonOrderDetail;
import com.ruoyi.system.domain.SalonService;
import com.ruoyi.system.mapper.SalonOrderMapper;
import com.ruoyi.system.mapper.SalonServiceMapper;
import com.ruoyi.system.mapper.SalonOrderDetailMapper;
import com.ruoyi.system.vo.OrderServiceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.ISalonOrderDetailService;

/**
 * 订单明细Service业务层处理
 * 
 * @author 23
 * @date 2022-03-31
 */
@Service("salonOrderDetailService")
public class SalonOrderDetailServiceImpl implements ISalonOrderDetailService {
    Map<String,List<SalonOrderDetail>> mapCache = new HashMap<>(); //缓存数据
    @Autowired
    private SalonOrderMapper salonOrderMapper;

    @Autowired
    private SalonServiceMapper salonServiceMapper;

    @Autowired
    private SalonOrderDetailMapper salonOrderDetailMapper;

    /**
     * 查询订单明细单条数据
     * 
     * @param orderServiceId 订单明细主键
     * @return 订单明细
     */
    @Override
    public SalonOrderDetail queryById(Integer orderServiceId){
        return this.salonOrderDetailMapper.queryById(orderServiceId);
    }

    /**
     * 查询所有订单明细
     * 
     * @param salonOrderDetail 订单管理
     * @return 订单管理
     */
    @Override
    public List<SalonOrderDetail> queryAll(SalonOrderDetail salonOrderDetail){
        return this.salonOrderDetailMapper.queryAll(salonOrderDetail);
    }

    /**
     * 新增订单明细
     * 
     * @param salonOrderDetail 订单管理
     * @return 结果
     */
    @Override
    public SalonOrderDetail insert(SalonOrderDetail salonOrderDetail){
        this.salonOrderDetailMapper.insert(salonOrderDetail);
        return salonOrderDetail;
    }

    /**
     * 修改订单管理
     * 
     * @param salonOrderDetail 订单管理
     * @return 结果
     */
    @Override
    public SalonOrderDetail update(SalonOrderDetail salonOrderDetail){
        this.salonOrderDetailMapper.update(salonOrderDetail);
        return this.queryById(salonOrderDetail.getOrderServiceId());
    }

    /**
     * 通过主键删除订单明细
     * 
     * @param orderServiceId 订单管理主键
     * @return 结果
     */
    @Override
    public boolean deleteById(Integer orderServiceId){
        return this.salonOrderDetailMapper.deleteById(orderServiceId);
    }

    /**
     * 步骤1：根据订单编号查询订单信息和业务关键信息
     * @param orderNo 订单编号
     * @return 订单信息，订单选中的服务
     */
    @Override
    public Map<String, Object> getOrderAndBusinessInformationByOrderNo(String orderNo) {
        this.createMapCache(orderNo);  // 创建下单数据缓存
        Map<String, Object> info = new HashMap<String,Object>();
        SalonOrder salonOrder = this.salonOrderMapper.queryOrderWithServiceListByOrderNo(orderNo);
        info.put("order",salonOrder);
        return info;
    }

    /**
     * 生成缓存数据_内存存储_HashMap
     * 步骤1_依赖方法1
     * @param orderNo
     * @return
     */
    private Map<String, List<SalonOrderDetail>> createMapCache(String orderNo){
        List<SalonOrderDetail> orderDetailList = new ArrayList<>();
        mapCache.put(orderNo,orderDetailList);
        return mapCache;
    }

    /**
     * 得到订单中服务明细列表
     * 步骤1_依赖方法2
     * @param orderNo
     * @return
     */
    private List<SalonService> getChoosingServiceOfOrder(String orderNo){
        SalonOrder salonOrder = this.salonOrderMapper.queryOrderWithServiceListByOrderNo(orderNo);
        return salonOrder.getServiceList();

    }

    /**
     * 步骤2：添加服务到订单明细
     * @param orderServiceVo 传值对象
     * 封装了orderNo和serviceNo
     * @return 订单明细
     */
    @Override
    public List<SalonService> enterLineOrderService(OrderServiceVo orderServiceVo) {
        String orderNo = orderServiceVo.getOrderNo();
        int orderId = this.salonOrderMapper.queryOrderWithServiceListByOrderNo(orderNo).getOrderId();
        SalonService salonService = this.salonServiceMapper.queryIdByServiceNo(orderServiceVo.getServiceNo());
        SalonOrderDetail salonOrderDetail = new SalonOrderDetail();
        salonOrderDetail.setOrderId(orderId);
        salonOrderDetail.setServiceId(salonService.getServiceId());
        List<SalonOrderDetail> orderDetailList = mapCache.get(orderNo);
        orderDetailList.add(salonOrderDetail);
        mapCache.put(orderNo,orderDetailList);
        return this.getEnterServiceList(orderNo);
    }

    /**
     * 根据orderNo从缓存加载serviceIds，得到订单明细集合
     * 步骤2_依赖方法
     * @param orderNo 订单编号
     * @return 订单明细集合
     */
    private List<SalonService> getEnterServiceList(String orderNo){
        List<SalonService> enterServiceList = new ArrayList<>();
        List<SalonOrderDetail> orderDetailList = mapCache.get(orderNo);
        for(SalonOrderDetail orderDetail : orderDetailList){
            enterServiceList.add(this.salonServiceMapper.selectSalonServiceByServiceId(orderDetail.getServiceId()));
        }
        return enterServiceList;
    }

    /**
     * 步骤3：确认下单
     * @param orderNo 订单编号
     */
    @Override
    public boolean ConfirmChooseService(String orderNo) {
        boolean flag = false;
        List<SalonOrderDetail> orderDetailList = mapCache.get(orderNo);
        return flag;
    }

    /**
     * 删除缓存数据_内存存储_HashMap
     * 步骤3_依赖方法
     * @param orderNo
     */
    private void removeMapCache(String orderNo) {
        Iterator<Map.Entry<String,List<SalonOrderDetail>>> iterator = mapCache.entrySet().iterator();
        while (iterator.hasNext()) {
            String key = iterator.next().getKey();
            if (orderNo.equals(key)) {
                iterator.remove();
            }
        }
    }

}
